package org.apache.ibatis.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author wangtbo
 * @date 2023/4/13 16:44
 * @description mybatis入口
 */
public class TestMybatis {

  public static void main(String[] args) throws IOException {
    String resource = "org/apache/ibatis/main/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory =
      new SqlSessionFactoryBuilder().build(inputStream);
    // sqlSessionFactory.getConfiguration().getInterceptors().add()
    try (SqlSession session = sqlSessionFactory.openSession()) {
      BlogMapper mapper = session.getMapper(BlogMapper.class);
      /*Blog blog = mapper.selectBlog(1L);
      System.out.println("userName:"+blog.getUserName());*/
      Blog user1 = new Blog("user1", 20);
      Blog user2 = new Blog("user2",21);
      List<Blog> userList = new ArrayList<>();
      user1.setId(120L);
      userList.add(user1);
      user2.setId(119L);
      userList.add(user2);
      //mapper.insertBlogBatch(userList);
      mapper.insertBlog(user1);
      System.out.println("LastInsertId1："+mapper.selectLastInsertId());
      mapper.insertBlog(user2);
      System.out.println("LastInsertId2："+mapper.selectLastInsertId());
      session.commit();
      System.out.println(user1.toString()+"\r\n"+user2.toString());
    }
  }
}
